<?php
/**
  * 前端文件配置
  *
  * @access public
  * @author virowin<virowin@gmail.com>
  * @version 1.0 Wed Apr 25 15:18:22 CST 2012
  * @package app
  */
//头文件引入
define(VIROWIN,1);
require_once('include.php');
$type = $_GET['type'];
empty($type) && $type = 'index';
if($type == 'reg'){
	$team_list = Biz_Team::getAllTeam();
	$user_list = Biz_User::getAllUser();
	$unselect_team = $team_list;
	if($user_list)
	foreach ($user_list as $per){
		unset($unselect_team[$per['teamid']]);
	}
	$smarty = new Lib_SmartyTpl();
	$smarty->assign('unselect_team',$unselect_team);
	$smarty->show('reg');
}elseif ($type == 'reg_sub'){
	if(!$_POST['email'] || !$_POST['username'] || !$_POST['password'] || !$_POST['password_c'] || !$_POST['teamid'] || !$_POST['randcode'] || $_POST['randcode'] != $_SESSION['randcode'] || $_POST['password']!= $_POST['password_c']){
		alert('输入错误','user.php?type=reg');
	}
	$array['email'] = trim(addslashes(strip_tags($_POST['email'])));
	$array['username'] = trim(addslashes(strip_tags($_POST['username'])));
	$array['password'] = md5($_POST['password']);
	$array['teamid'] = intval($_POST['teamid']);
	$array['joindate'] = date('Y-m-d H:i:s');
	if(Biz_User::checkTeamId($array['teamid'])){
		Data_User::add_one($array);
		alert('注册成功','index.php');
	}else{
		alert('该球队已被其他人选择','user.php?type=reg');
	}
}elseif ($type == 'login'){
	$smarty = new Lib_SmartyTpl();
	$smarty->show('login');
}elseif ($type == 'login_sub'){
	if(!$_POST['email'] || !$_POST['password']|| !$_POST['randcode'] || $_POST['randcode'] != $_SESSION['randcode'] ){
		alert('输入错误','user.php?type=login');
	}
	$array['email'] = trim(addslashes(strip_tags($_POST['email'])));
	$array['password'] = md5($_POST['password']);
	if($array = Biz_User::getUserFilter($array)){
		$_SESSION['userinfo'] = $array;
		alert('登陆成功','user.php');
	}else{
		alert("密码错误",'user.php?type=login');
	}
}elseif ($type == 'tactics'){
	$gamecode = intval($_GET['gameid']);
	$teamid = intval($_GET['teamid']);
	if($_SESSION['userinfo']['teamid'] != $teamid) alert('不是你的球队','user.php');
	if($schedule = Biz_Schedule::checkTeamGame($teamid,$gamecode)){
		$player_list = Biz_Player::getPlayerList(array('teamid'=>$teamid));
		foreach ($player_list as $per){
			$players [$per['id']] = $per;
		}
		unset($player_list);
		$team_list = Biz_Team::getAllTeam();
		$smarty = new Lib_SmartyTpl();
		$smarty->assign('schedule',$schedule);
		$smarty->assign('player_list',json_encode($players));
		$smarty->assign('team_list',$team_list);
		$smarty->show('tactics');
	}else{
		alert('不是你的比赛','user.php');
	}
}elseif ($type == 'index'){
	if($_SESSION['userinfo']){
		$userinfo = Biz_User::getUserFilter(array('id'=>$_SESSION['userinfo']['id']));
		$smarty = new Lib_SmartyTpl();
		$team_list = Biz_Team::getAllTeam();
		$schedule = Biz_Schedule::getSchedule(array('team'=>$userinfo['teamid']),'datetime asc',5);
		$smarty->assign('schedule',$schedule);
		$smarty->assign('userinfo',$userinfo);
		$smarty->assign('team_list',$team_list);
		$smarty->show('user');
	}else{
		alert("请先登录",'user.php?type=login');
	}
}